﻿using Comm.Model;
using Comm.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Comm.Web.Action
{
    /// <summary>
    /// BookList 的摘要说明
    /// </summary>
    public class BookList : BaseHander
    {
        BLL.BooksService bllbook = new BLL.BooksService();
        BLL.PublishersService bllpublish = new BLL.PublishersService();
        protected string strtype = string.Empty;
        public override void SonLoad()
        {
            string type =Request.Params["type"];
            switch (type) 
            {
                case "view":
                    CreateStaticPage();
                    break;
                case "Edit":
                    CreateViewStaticPage();
                    break;
                case "page":
                    getPageData();
                    break;
            }
        }

        public void CreateStaticPage() 
        {
            try
            {
                strtype = "true";
                int bookId = int.Parse(Request.Form["bookId"]);
                string htmlPath = bllbook.CreateStaticPage(bookId,strtype);
                AjaxMsgHelper.AjaxMsg("ok", "生成成功！~~",JsonHelper.Serialize(htmlPath));
            }
            catch (Exception ex) 
            {
                AjaxMsgHelper.AjaxMsg("err",ex.Message);
            }
        }

        public void CreateViewStaticPage()
        {
            try
            {
                strtype = "false";
                int bookId = int.Parse(Request.Form["bookId"]);
                string htmlPath = bllbook.CreateStaticPage(bookId,strtype);
                AjaxMsgHelper.AjaxMsg("ok", "生成成功！~~", JsonHelper.Serialize(htmlPath));
            }
            catch (Exception ex)
            {
                AjaxMsgHelper.AjaxMsg("err", ex.Message);
            }
        }

        public void getPageData()
        {
            string strPageIndex = context.Request.Form["pi"];
            int pageIndex = 1;
            int pageCount = 0;
            int rowCount = 0;
            pageCount = bllbook.GetPageCount(0, 10);
            rowCount = bllbook.GetRowCount();
            //如果页码格式 错误，则默认 显示 第一页数据
            if (!ValidateHelper.IsNum(strPageIndex))
            {
                pageIndex = 1;
            }
            else
            {
                pageIndex = int.Parse(strPageIndex);
            }
            IList<Model.Publishers> list1 = bllpublish.GetPublishersList();
            IList<Model.Books> list = bllbook.GetPageData(pageIndex, 10, 0, "", "");
            common.PagedAjaxData<Books> pageDataList = new common.PagedAjaxData<Books>()
            {
                PageIndex = pageIndex,
                PageCount = pageCount,
                RowCount = rowCount,
                PagedList = list,
                PublishList=list1
            };
            AjaxMsgHelper.AjaxMsg("ok", "数据加载成功~~~", JsonHelper.Serialize(pageDataList));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}